package net.huashitong.synthetical.showView.web;

import com.alibaba.fastjson.JSONObject;
import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.synthetical.showView.filter.ProjectFilter;
import net.huashitong.synthetical.showView.service.SyntheService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.*;

import static com.alibaba.fastjson.JSON.parseObject;
import static net.huashitong.synthetical.showView.util.HttpsUtils.doGet;
import static net.huashitong.synthetical.showView.util.URLstatic.*;
import static net.huashitong.synthetical.showView.util.commonUtil.strDealWith;

@Controller
@RequestMapping("/synthindex")
public class SyntheIndexCtrl extends JsonBaseController {

    Logger log = LoggerFactory.getLogger(SyntheIndexCtrl.class);

    @Autowired
    private SyntheService syntheService;

    @Override
    protected String getView(String s) {
        return "/modules/synthetical/" + s;
    }

    @RequestMapping(method = RequestMethod.GET)
    private String initIndex() {

        return getView("index");
    }
    //详情页
    @RequestMapping(value = "/details")
    private String showDetail() {
        return getView("detail");
    }

    @RequestMapping(value = "/warning")
    private String showWarningDetail() {
        return getView("warning");
    }

    /**
     * 项目进度时间表
     *
     * @param projectId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getprojectschedule", method = RequestMethod.GET)
    public Message getProjectSchedule(String projectId) {
        Map<String, Object> params = new HashMap<>();
        if (StringUtils.isBlank(projectId)) {
            //默认中海创项目
            params.put("projectId", "20180312100000000352");
        } else {
            params.put("projectId", projectId);
        }
        String url = GET_PRO_SEC;
        String url2 = GET_BASIC_SITUATION;
        Map<String, Object> resMap = syntheService.getProjectSchedule(url, url2, params);
        if ("-".equals(resMap.get("nodes"))) {
            return coverMessage("201", "没有查询到此项目", resMap);
        } else {
            return coverMessage("200", "成功", resMap);
        }
    }

    /**
     * 项目时间情况总览
     *
     * @param projectId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getprojectprogresstime", method = RequestMethod.GET)
    public Message getProjectProgressTime(@RequestParam String projectId) {
        //重大项目 项目简单时间信息 接口
        String url = GET_PRO_TIME;
        Map<String, Object> params = new HashMap<>();
        if (StringUtils.isBlank(projectId)) {
            //默认中海创项目
            params.put("projectId", "20180312100000000352");
        } else {
            params.put("projectId", projectId);
        }
        Map<String, Object> resMap = syntheService.getProjectProgressTime(url, params);
        if ("-".equals(resMap.get("projectName"))) {
            return coverMessage("201", "没有查询到此项目", resMap);
        } else {
            return coverMessage("200", "成功", resMap);
        }

    }


    /**
     * 头部基本信息
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getbasesituation", method = RequestMethod.POST)
    public Message getBasicSituation(@RequestBody ProjectFilter filter) throws ParseException {
        String projectId = filter.getProjectId();
        if (StringUtils.isBlank(filter.getProjectId())) {
            //默认中海创Id
            projectId = "20180312100000000352";
        }
        String url = GET_BASIC_SITUATION;
        Map<String, Object> params = new HashMap<>();
        //带参数？？？
        Map<String, Object> resMap = syntheService.getbasesituation(url, params);
        List<Map<String, Object>> list = syntheService.getBuildProgress(projectId);
        resMap.put("progress2", list);
        return coverMessage("200", "成功", resMap);
    }

    @ResponseBody
    @RequestMapping(value = "/getbuilddetailbynodeid", method = RequestMethod.POST)
    public Message getBuildDetailByNodeId(@RequestBody ProjectFilter filter) {

        String projectId = filter.getProjectId();
        String nodeId = filter.getNodeId();

        //添加节点信息
        Map<String, Object> param = new HashMap<>();
        param.put("nodeId", nodeId);
        param.put("projectId", projectId);

        Map<String,Object> resMap = new HashMap<>();
        JSONObject jsonOb = doGet(GET_NODE_DETAIL, param);
        JSONObject object1 = jsonOb.getJSONObject("data");
        String strJsonObj = object1.getJSONArray("smallNodes").get(0).toString();
        Map<String, Object> smallNode = parseObject(strJsonObj, HashMap.class);
        resMap.put("resUser", strDealWith(smallNode.get("resUser")));
        resMap.put("projectState", "已完成");
        resMap.put("datetime", strDealWith(smallNode.get("factFinishDate")));
        resMap.put("nodeTime", strDealWith(smallNode.get("factTime")));

        return coverMessage("200", "成功", resMap);

    }

}
